/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import conexao.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import modelo.Fornecedor;

public class FornecedorDAO {
    
    private Connection conexao;
    private PreparedStatement stmt;
    private ResultSet rs;
    private String sql;

    public FornecedorDAO() throws Exception {
        try {
            this.conexao = Conexao.getConexao();
        } catch (Exception ex) {
            throw new Exception("Erro: " + ex.getMessage());
        }
    }

    public void finalizar() throws Exception {
        try {
            Conexao.closeConexao(this.conexao, this.stmt, this.rs);
        } catch (SQLException ex) {
            throw new Exception("Erro ao finalizar " + ex);
        }
    }
    
    public void incluir(Fornecedor fornecedor) throws Exception {
        try {
            sql = "insert into FORNECEDOR (ID, NOME, ENDERECO, TELEFONE, EMAIL) values (?, ?, ?, ?, ?)";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, fornecedor.getId());
            stmt.setString(2, fornecedor.getNome());
            stmt.setString(3, fornecedor.getEndereco());
            stmt.setString(4, fornecedor.getFone());
            stmt.setString(5, fornecedor.getEmail());
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao incluir " + ex);
        }
    }

    public void alterar(Fornecedor fornecedor) throws Exception {
        try {
            sql = "update FORNECEDOR set NOME=? where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setString(1, fornecedor.getNome());
            stmt.setInt(2, fornecedor.getId());
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao alterar " + ex);
        }
    }

    public void excluir(Integer id) throws Exception {
        try {
            sql = "delete from FORNECEDOR where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, id);
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao excluir " + ex);
        }
    }

    public Fornecedor consultar(Integer id) throws Exception {
        try {
            sql = "select * from FORNECEDOR where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            Fornecedor fornecedor = new Fornecedor();
            if (rs.next()) {
                fornecedor.setId(rs.getInt("id"));
                fornecedor.setNome(rs.getString("nome"));
                fornecedor.setEndereco(rs.getString("endereco"));
                fornecedor.setFone(rs.getString("telefone"));
                fornecedor.setEmail(rs.getString("email"));
            }
            rs.close();
            stmt.close();
            return fornecedor;
        } catch (SQLException ex) {
            throw new Exception("Erro ao consultar " + ex);
        }    
    }     
    
    public List<Fornecedor> listar() throws Exception {
        try {
            sql = "select * from FORNECEDOR order by NOME";
            stmt = conexao.prepareStatement(sql);
            rs = stmt.executeQuery();
            List<Fornecedor> fornecedores = new ArrayList<Fornecedor>();
            
            while (rs.next()) {
                Fornecedor fornecedor = new Fornecedor();
                fornecedor.setId(rs.getInt("id"));
                fornecedor.setNome(rs.getString("nome"));
                fornecedor.setEndereco(rs.getString("endereco"));
                fornecedor.setFone(rs.getString("telefone"));
                
                fornecedores.add(fornecedor);
            }
            rs.close();
            stmt.close();
            return fornecedores;
        } catch (SQLException ex) {
            throw new Exception("Erro ao listar " + ex);
        }
    }      
}
